//
//  WinnerLayer.cpp
//  firebkball
//
//  Created by ren jian on 18/5/29.
//
//

#include "WinnerLayer.h"
#include "WelcomeLayer.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "stdtostring.h"
#endif

bool WinnerLayer::init()
{
    if (!Layer::init()) {
        return false;
    }
    m_lastTime = 10;
    
    // TODO::测试数据插入
    UserDataManager::getInstance()->battleData->score = 50;
    
    m_size = Director::getInstance()->getWinSize();
    this->ignoreAnchorPointForPosition(false);
    this->setAnchorPoint(Vec2(0.5, 0.5));
    this->setContentSize(m_size);
    this->setPosition(m_size/2);
    
    
    m_controller = CSLoader::createNode("res/winnerView.csb");
    m_controller->setAnchorPoint(Vec2(0.5, 0.5));
    m_controller->setPosition(m_size/2);
    this->addChild(m_controller);
    
    auto l_box = (Node *)m_controller->getChildByName("l_box");
    normalPrizeNode = l_box->getChildByName("l_jpBox");
    bigPrizeNode = l_box->getChildByName("l_jpBox_big");
    
    btn_chujiang = (cocos2d::ui::Button *)m_controller->getChildByName("btn_cj");
    btn_goon = (cocos2d::ui::Button *)m_controller->getChildByName("btn_goon");
    
    btn_chujiang->addClickEventListener(CC_CALLBACK_1(WinnerLayer::getPrize, this));
    btn_goon->addClickEventListener(CC_CALLBACK_1(WinnerLayer::gameGoon, this));
    
    return true;
}

void WinnerLayer::onEnter()
{
    Layer::onEnter();
    
    this->normalPrize();
}

void WinnerLayer::bigPrize()
{
    normalPrizeNode->removeFromParent();
    bigPrizeNode->setVisible(true);
    
    Sprite *sp_score = (Sprite *)bigPrizeNode->getChildByName("sp_getjb");
    cocos2d::ui::Text *l_scoreLab = (cocos2d::ui::Text*)sp_score->getChildByName("lab_score");
    string l_score = StringUtils::format("%d", UserDataManager::getInstance()->battleData->score);
    l_scoreLab->setString(l_score);
    
    btn_goon->setEnabled(false);
}

void WinnerLayer::normalPrize()
{
    bigPrizeNode->removeFromParent();
    normalPrizeNode->setVisible(true);
    
    Sprite *sp_score = (Sprite *)normalPrizeNode->getChildByName("sp_getjp");
    cocos2d::ui::Text *l_scoreLab = (cocos2d::ui::Text*)sp_score->getChildByName("lab_score");
    string l_score = StringUtils::format("%d", UserDataManager::getInstance()->battleData->score);
    l_scoreLab->setString(l_score);
    
    // 倒计时
    Node *l_node = normalPrizeNode->getChildByName("nd_daojishi");
    Label *l_timeLab = Label::createWithTTF(to_string(m_lastTime-1), "font/002.ttf", 100);
    l_timeLab->setTextColor(Color4B(82, 149, 20, 255));
    l_node->addChild(l_timeLab);
    
    auto l_fireballTimer = ProgressTimer::create(Sprite::create("imgs/balltimer.png"));
    l_fireballTimer->setAnchorPoint(Vec2(0.5, 0.5));
    l_fireballTimer->setPosition(0, 0);
    l_fireballTimer->setType(ProgressTimer::Type::RADIAL);
    l_fireballTimer->setReverseDirection(true);
    l_node->addChild(l_fireballTimer, 9);
    l_fireballTimer->setPercentage(100);
    
    l_fireballTimer->runAction(ProgressTo::create(1, 0));
    l_fireballTimer->runAction(Repeat::create(Sequence::create(
                                                               CallFunc::create([=](){
        l_fireballTimer->setPercentage(100);
        m_lastTime--;
        l_timeLab->setString(to_string(m_lastTime));
        l_timeLab->setScale(0.9);
        l_timeLab->runAction(EaseBackOut::create(ScaleTo::create(0.2, 1)));
        
    }),
                                                               ProgressTo::create(1, 0),
                                                               CallFunc::create([=](){
        if (m_lastTime==0) {
            // TODO::超时
            CCLOG("超时");
        }
    })
                                                               , NULL), 10));
}

void WinnerLayer::getPrize(cocos2d::Ref *ref)
{
    //TODO::领奖逻辑
    this->setVisible(false);
    Director::getInstance()->getRunningScene()->addChild(WelcomeLayer::create());
    this->removeFromParent();
}

void WinnerLayer::gameGoon(cocos2d::Ref *ref)
{
    if (UserDataManager::getInstance()->userData->coin<UserDataManager::getInstance()->userData->startCoinNumber) {
        return;
    }
    UserDataManager::getInstance()->addCoin(-UserDataManager::getInstance()->userData->startCoinNumber);
    
    //TODO::游戏继续
    
}

void WinnerLayer::onExit()
{
    Layer::onExit();
}




